﻿using System;
using System.Collections.Generic;
using System.Text;
using HHL_DaoAcc.DataObject;
using HHL_Util;
using HHL_Acc.HHL_DaoAcc.DataObject;
using HHL_Acc.HHL_EntityAcc;
using System.Windows.Forms;

namespace HHL_Acc.HHL_BussAcc.DataObject
{
    class SmiExportsBus
    {
        private SmiExportsDao smiExportsDao;

        public SmiExportsBus()
        {
            smiExportsDao = new SmiExportsDao();
        }

        public List<SmiExports> GetSmiExportDetails(int size,int skip)
        {
            return (List<SmiExports>)smiExportsDao.GetSmiExportDetails(size,skip);
        }
        public int GetTotalRecord()
        {
           int i= smiExportsDao.GetTotalRecord();
            return i;
        }
        // hàm xuất kho
        public int InsertSmiExport(SmiExports dto, List<SmiExportDetails> smiExportDetailses)
        {

            // Insert table Smi Export Details
            int result = smiExportsDao.InsertSmiExports(dto);
            if (result <= 0)
            {
                // return if insert unsuccessful
                return result;
            }
            SmiExportDetailsDao smiExportDetailsDao = new SmiExportDetailsDao();
            if (smiExportDetailses != null && smiExportDetailses.Count > 0)
            {

                foreach (SmiExportDetails smiExportDetail in smiExportDetailses)
                {
                    smiExportDetailsDao.InsertSmiExportDetail(smiExportDetail);
                }
            }
            return 1;
            //smiImportDetailsDao.
        }
        // update smiExport
        public int UpdateSmiExport(SmiExports smiExports, List<SmiExportDetails> smiExportDetailsList)
        {
            // Insert table Smi Export Details
            // SqlTransaction sqlTransaction = new SqlTransaction()

            int result = smiExportsDao.UpdateSmiExports(smiExports);
            if (result <= 0)
            {
                // return if insert unsuccessful
                return result;
            }
            SmiExportDetailsDao smiExportDetailsDao = new SmiExportDetailsDao();

            // delete all old record
            int deleteRslt = smiExportDetailsDao.DeleteSmiExportDetailByImpId(smiExports.ExpID);
            if (deleteRslt == -1)
            {
                return -1;
            }
            // insert new 
            if (smiExportDetailsList != null && smiExportDetailsList.Count > 0)
            {

                foreach (SmiExportDetails smiExportDetail in smiExportDetailsList)
                {
                    result = smiExportDetailsDao.InsertSmiExportDetail(smiExportDetail);
                    if (result <= 0)
                    {
                        // return if insert unsuccessful
                        return result;
                    }
                }
            }
            return 1;
        }

        
    }
}
